package dao;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import bean.QuestionBean;

public class DaQuestionXML implements Dao {

	private QuestionBean questionBean;
	private ArrayList<Integer> arrInt;

	private final class IdHandler extends DefaultHandler {

		boolean id = false;

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (qName.equalsIgnoreCase("ID")) {
				id = true;
			}

		}

		public void characters(char ch[], int start, int length)
				throws SAXException {

			if (id) {
				String s = new String(ch, start, length);
				arrInt.add(Integer.parseInt(s));
				id = false;
			}
		}
	}

	private final class QuestionBeanHandler extends DefaultHandler {

		public QuestionBeanHandler(int id) {
			this.idCurent = id;
		}

		boolean id = false;
		boolean question = false;
		boolean answer = false;
		boolean answer1 = false;
		boolean answer2 = false;
		boolean answer3 = false;
		boolean answer4 = false;
		int idCurent;
		boolean sw = false;

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {

			System.out.println("Start Element :" + qName);

			if (qName.equalsIgnoreCase("ID")) {
				id = true;
			}

			if (qName.equalsIgnoreCase("question")) {
				question = true;
			}
			if (qName.equalsIgnoreCase("answer")) {
				answer = true;
			}

			if (qName.equalsIgnoreCase("answer1")) {
				answer1 = true;
			}

			if (qName.equalsIgnoreCase("answer2")) {
				answer2 = true;
			}
			if (qName.equalsIgnoreCase("answer3")) {
				answer3 = true;
			}

			if (qName.equalsIgnoreCase("answer4")) {
				answer4 = true;
			}

		}

		public void characters(char ch[], int start, int length)
				throws SAXException {

			if (id) {

				String s = new String(ch, start, length);
				int i = Integer.parseInt(s);
				if (i == idCurent) {
					questionBean.setId(Integer.parseInt(s));
					sw = true;
				} else {
					sw = false;
				}

				id = false;
			}
			if (sw) {
				if (question) {

					questionBean.setQuestion(new String(ch, start, length));
					question = false;
				}

				if (answer) {
					questionBean.setTrueResponse(new String(ch, start, length));
					answer = false;
				}

				if (answer1) {
					questionBean.setResponse1(new String(ch, start, length));
					answer1 = false;
				}
				if (answer2) {
					questionBean.setResponse2(new String(ch, start, length));
					answer2 = false;
				}
				if (answer3) {
					questionBean.setResponse3(new String(ch, start, length));
					answer3 = false;
				}
				if (answer4) {
					questionBean.setResponse4(new String(ch, start, length));
					answer4 = false;
				}
			}

		}
	}

	private void parse(DefaultHandler handle) {

		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser saxParser = null;
		try {
			saxParser = factory.newSAXParser();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			saxParser.parse("c:\\question.xml", handle);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public ArrayList<Integer> getAllId() {

		arrInt = new ArrayList<Integer>();
		parse(new IdHandler());
		return arrInt;
	}

	@Override
	public QuestionBean getQuestionBeanById(int id) {
		questionBean = new QuestionBean();
		parse(new QuestionBeanHandler(id));
		return questionBean;
	}

}
